package com.extremeforce.adapter;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.extremeforce.R;
import com.extremeforce.function.GetNetImage;
import com.extremeforce.module.GetCouponActivity;
import com.extremeforce.module.MyCouponDetailInfoActivity;

public class MyCouponListAdapter extends BaseAdapter{
	private ArrayList<HashMap<String,Object>> list;
	private Context context ;
	public MyCouponListAdapter(ArrayList<HashMap<String,Object>> list,Context context){
		this.list = list;
		this.context = context;		
	}
	

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return null;
	}

	public long getItemId(int position) {
		return 0;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		
		LayoutInflater mInflater = LayoutInflater.from(context);
		
		final HashMap<String,Object> map = list.get(position);
		
		convertView = mInflater.inflate(R.layout.mycouponitem, null);
		
		TextView titleTextView = (TextView)convertView.findViewById(R.id.mycouponTitle);
		
		titleTextView.setText(String.valueOf(map.get("title")));
		
		TextView descTextView = (TextView)convertView.findViewById(R.id.mycouponDesc);
		descTextView.setText(String.valueOf(map.get("desc")));
		
		ImageView couponImageView = (ImageView)convertView.findViewById(R.id.mycouponImage);
		
		byte[] array = (byte[])map.get("image");
		couponImageView.setImageBitmap(GetNetImage.byteArrayToImage(array));
		
		TextView idTextView = (TextView)convertView.findViewById(R.id.mycouponid);
		idTextView.setText(String.valueOf(map.get("id")));
		
		
		
		
		
		convertView.setOnClickListener(
				new View.OnClickListener() {
					
					public void onClick(View v) {
						
						Bundle bundle = new Bundle();
						bundle.putString("title",String.valueOf(map.get("title")));
						bundle.putString("desc",String.valueOf(map.get("desc")));
						bundle.putByteArray("image",(byte[])map.get("image"));
						bundle.putString("serial",String.valueOf(map.get("serial")));
						bundle.putString("coupon_id",String.valueOf(map.get("id")));
						bundle.putString("date",String.valueOf(map.get("date")));
						
						Intent intent = new Intent();
						intent.setClass(context,MyCouponDetailInfoActivity.class);
						intent.putExtras(bundle);
						context.startActivity(intent);
					}
				}
				);
		
		
		return convertView;
	}
}
